using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using MySql.Data.MySqlClient;
namespace Lin {
	class Program2 {
		//create table dict(id int primary key,name varchar(100))
		static string Consolidate(int dict,string[] arr) {
			List<string> ret=new List<string>();
			foreach(string str in arr)
			{
				if(Nod.InnerText(str).Trim()=="")
					continue;
				string itm=str.ToLower();
				ret.Add(itm);
			}
			string sret=string.Join("; ",ret.ToArray());
			return sret;
		}
		static void Main() {
			Cache cac=new Cache("lin");
			string bas=cac["tlu"];
			MDAL dal=new MDAL();
			string last=dal.Scalar("select wrd from slowo order by wrd desc limit 1").ToString();
			string[] words=File.ReadAllLines("wordsEn.txt");
			dal.NonQry("truncate table list");

			//foreach(string wrd in words)
			//  dal.NonQry("insert into list(wrd)values('{0}')",MySqlHelper.DoubleQuoteString(wrd));
			//return;

			foreach(string wrd in words)
			{
				if(wrd.CompareTo(last)<=0)
					continue;
				//System.Threading.Thread.Sleep(500);

				string cont=cac.Get(bas+wrd,100000);
				string[] dicts=Nod.Dbls(cont,"<div id=\"lang-1");
				foreach(string dict in dicts)
				{
					//string[] divs1=dict.Split(new string[] { "<div class=\"clear\"></div>" },StringSplitOptions.None);
					string[] divs1=Nod.Dbls(dict,"<div class=\"grid_12 dictName alpha omega\">");
					string[] divs2=Nod.Dbls(dict,"<div class=\"grid_12 alpha translate omega\">");
					if(divs1.Length!=divs2.Length)
					{
						Console.WriteLine(wrd);
						continue;
					}
					else
					{
						for(int i=0;i<divs1.Length;i++)
						{
							int dId=int.Parse(Nod.Rx(divs1[i],"<div class=\"dict-(\\d+?)\">"));
							string dname=Nod.Rx(divs1[i],"<div class=\"dict-\\d+\">(.+?)</div>").Trim();

							dal.NonQry("insert ignore into dict(id,name)values({0},'{1}')",dId,MySqlHelper.DoubleQuoteString(dname));

							string def1=divs2[i];
							string[] ps=Nod.Rxs(def1,"<p class=\"dict.ef\">(.*?)<br ?/></p>");
							string entry=Consolidate(dId,ps);
							dal.NonQry("insert into slowo(wrd,dict,tran)values('{0}',{1},'{2}')",MySqlHelper.DoubleQuoteString(wrd),dId,MySqlHelper.DoubleQuoteString(entry));
						}
					}
				}
			}
		}
	}
}
